<template>
  <div class="address">
    <div style="text-align: left; margin-bottom: 5px">
      <el-button
        size="small"
        icon="el-icon-plus"
        type="primary"
        @click="addAddress"
        >新增地址
      </el-button>
      <el-button
        size="small"
        icon="el-icon-delete"
        type="danger"
        @click="delAddress"
        >删除地址
      </el-button>
    </div>
    <el-table
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      :header-cell-style="{
        background: '#fafafa',
        color: '#000000D9',
        fontSize: '14px',
        padding: '10px 0',
        border: '1px solid #eee',
      }"
      :cell-style="{
        color: '#606266',
        fontSize: '12px',
        padding: '6px 0',
      }"
      :data="tableData"
      style="width: 100%"
    >
      <template slot="empty">
        <img
          class="empty-img"
          src="@/assets/imgs/zanwushuju.svg"
          style="width: 64px; height: 50px"
          alt=""
        />
        <div class="noCart">暂无数据</div>
      </template>
      <el-table-column type="selection" align="center"></el-table-column>
      <el-table-column
        type="index"
        align="center"
        label="序号"
        width="80"
      ></el-table-column>
      <el-table-column
        align="center"
        label="姓名"
        prop="name"
        width="120"
      ></el-table-column>
      <el-table-column
        align="center"
        label="联系电话"
        prop="Telephone"
      ></el-table-column>
      <el-table-column
        align="center"
        label="邮编"
        prop="postal_code"
      ></el-table-column>

      <el-table-column
        align="center"
        label="所在区域"
        prop="address_one"
      ></el-table-column>
      <el-table-column
        align="center"
        label="详细地址"
        prop="address_two"
      ></el-table-column>
      <el-table-column align="center" label="默认地址" prop="status">
        <template slot-scope="scope">
          <el-switch
            @change="changeVal(scope.row)"
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="380px" align="left">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            @click="handleEdit(scope.row)"
            >编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.addr_deliver_id)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <Pagination :child-msg="pageparm" @callFather="callFather"></Pagination>
    <addressModal
      ref="form"
      @submit="submitClick"
      :dialogVisible.sync="dialogVisible"
      :dialogTitle="dialogTitle"
      :editData="editData"
      @closeForm="closeDialog"
    ></addressModal>
  </div>
</template>

<script>
import {
  getAddressList,
  addDeliverAdd,
  deliverUpdate,
  deleteAddress,
  makeDefault,
} from "@/api/address";
import addressModal from "@/views/membe/components/addressModal.vue";
import StockOrder from "@/views/warehouse/stockUp/components/StockOrder.vue";

export default {
  name: "index",
  components: {
    StockOrder,
    addressModal,
  },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "",
      searchData: {},
      search_visible: true,
      showIndex: [],
      //---------------------------------------
      loading: false,
      formInline: {
        page: 1,
        limit: 20,
      },
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 20,
        total: 0,
      },
      tableData: [],
      editData: null,
      multipleSelection: [],
    };
  },
  created() {
    this.getAddressList();
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = [];
      val.forEach((item) => {
        this.multipleSelection.push(item.addr_deliver_id);
      });
    },
    submitClick(val) {
      if (val.addr_deliver_id) {
        this.deliverUpdate(val);
      } else {
        this.addDeliverAdd(val);
      }
    },
    async changeVal(val) {
      const res = await makeDefault({
        addr_deliver_id: val.addr_deliver_id,
        status: val.status,
      });
      if (res.data.status === 200) {
        this.$message({
          type: "success",
          message: res.data.msg,
        });
        this.getAddressList();
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    handleDelete(ids) {
      this.$confirm("确定操作吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.deleteAddress({ addr_deliver_id: [ids] });
        })
        .catch(() => {});
    },
    delAddress() {
      if (this.multipleSelection.length > 0) {
        this.$confirm("确定操作吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            this.deleteAddress({ addr_deliver_id: this.multipleSelection });
          })
          .catch(() => {});
      } else {
        this.$message({
          type: "warning",
          message: "请选择地址",
        });
      }
    },
    addAddress() {
      this.dialogVisible = true;
      this.dialogTitle = "新增地址";
      this.$refs["form"].form = {};
    },
    handleEdit(row) {
      this.dialogVisible = true;
      this.dialogTitle = "编辑地址";
      this.editData = row;
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
    },
    //请求列表
    async getAddressList() {
      this.loading = true;
      try {
        let obj = {};
        Object.assign(obj, this.searchData, this.formInline);
        const res = await getAddressList(obj);
        if (res.data.status === 200) {
          this.tableData = res.data.data.data;
          this.pageparm.currentPage = res.data.data.current_page;
          this.pageparm.total = res.data.data.total;
          this.pageparm.pageSize = res.data.data.per_page;
        } else {
          this.$message.error(res.data.msg);
        }
        this.loading = false;
      } catch (error) {
        this.loading = false;
        this.$message.error("系统错误");
      }
    },
    //添加发货地址
    async addDeliverAdd(obj) {
      const res = await addDeliverAdd(obj);
      if (res.data.status === 200) {
        this.$message({
          type: "success",
          message: res.data.msg,
        });
        this.getAddressList();
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //修改发货地址
    async deliverUpdate(obj) {
      const res = await deliverUpdate(obj);
      if (res.data.status === 200) {
        this.$message({
          type: "success",
          message: res.data.msg,
        });
        this.getAddressList();
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //删除地址
    async deleteAddress(ids) {
      const res = await deleteAddress(ids);
      if (res.data.status === 200) {
        this.$message({
          type: "success",
          message: res.data.msg,
        });
        this.getAddressList();
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //分页插件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getAddressList();
    },
  },
};
</script>

<style scoped>
.address {
  padding: 15px 10px;
}
</style>
